package com.tek.ding.faceding.ui.face;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tek.ding.faceding.R;
import com.tek.ding.faceding.bean.Members;
import com.tek.ding.faceding.utils.NepUtil;

import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class MemberAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private LayoutInflater layoutInflater;
    private Context context;
    private RecyclerView recyclerView;
    private List<Members> memberList;

    public MemberAdapter(Context context, RecyclerView recyclerView, List<Members> memberList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.recyclerView = recyclerView;
        this.memberList = memberList;
    }

    public void setData(List<Members> photoList) {
        this.memberList.clear();
        this.memberList = photoList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_member, parent, false);
        view.setOnClickListener(this);
        MemberViewHolder photoViewHolder = new MemberViewHolder(view);
        return photoViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Members member = memberList.get(position);
        MemberViewHolder viewHolder = (MemberViewHolder) holder;
        viewHolder.tvName.setText("会员姓名：".concat(NepUtil.nullToSpace(member.getFullName())));
        if (member.getFaceUpStatus() != null && member.getFaceUpStatus().equals(1)) {
            viewHolder.button.setText("人脸已录入");
            viewHolder.button.setBackgroundResource(R.drawable.btn_face_on);
        } else {
            viewHolder.button.setText("人脸未录入");
            viewHolder.button.setBackgroundResource(R.drawable.btn_face_un);
        }
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isEmpty(memberList)) {
            return 0;
        }
        return memberList.size();
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            int childAdapterPosition = recyclerView.getChildAdapterPosition(view);
            onItemClickListener.onItemClickListener(memberList.get(childAdapterPosition));
        }
    }

    class MemberViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvParentName;
        TextView button;

        public MemberViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvParentName = itemView.findViewById(R.id.tvParentName);
            button = itemView.findViewById(R.id.btnFace);
        }
    }

    public OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClickListener(Members photo);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
